##
#   Project: gmediacatalog - Catalog your media to find them later
#    Author: Fabio Castelli <muflone@vbsimple.net>
# Copyright: 2010 Fabio Castelli
#   License: GPL-2+
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
# 
# On Debian GNU/Linux systems, the full text of the GNU General Public License
# can be found in the file /usr/share/common-licenses/GPL-2.
##

import threading

import gtk.gdk

from constants import *
from get_file_icon import get_icon_from_names, get_stock_icon

class VolumeLoader(threading.Thread):
  def __init__(self, connection, volume_id, volume_root, volume_name, 
               treeVolumes, when_completed):
    threading.Thread.__init__ (self)
    self.name = volume_id
    self.connection = connection
    self.volume_id = volume_id
    self.volume_root = volume_root
    self.volume_name = volume_name
    self.treeVolumes = treeVolumes
    self.__cancel = False
    self.when_completed = when_completed

  def run(self):
    paths = {}
    curPaths = self.connection.select_paths(self.volume_id)
    folderRoot = None
    ## Cycle paths for current volume
    icon_refresh = get_stock_icon('gtk-refresh')
    icon_disc = get_stock_icon('gtk-cdrom')
    icon_folder = get_stock_icon('gtk-directory')
    for rowPath in curPaths:
      # Remove volume prefix from path
      pathRelative = rowPath['paths_uri'][len(self.volume_root):]
      #print rowPath['paths_uri'], self.volume_root, pathRelative
      # Has it a relative path? (/subFolder)
      if pathRelative:
        # It has subFolder
        if '/' in pathRelative:
          pathParent, pathChild = pathRelative.rsplit('/', 1)
        else:
          pathParent = '/'
          pathChild = pathRelative
        # No parent, assume root
        if not pathParent:
          pathParent = '/'
        # Find parent folder
        if paths.has_key(pathParent):
          parent = paths[pathParent]
        else:
          # This SHOULD never happen!
          # Parent folder will be inserted before children
          print 'no', pathParent
          parent = None
      else:
        # Empty relative path (root of volume)
        parent = None
        pathRelative = '/'
        # Set volume name for root instead of folder name
        pathChild = self.volume_name

      # Add new node (volume or folder)
      subFolder = self.treeVolumes.append(parent, [
        pathChild, 
        pathRelative != '/' and (rowPath['paths_icon'] and 
          get_icon_from_names(str(rowPath['paths_icon']).split(';')) or 
          icon_folder) or icon_refresh,
        rowPath['paths_id'],
        pathRelative != '/' and ITEM_PATH or ITEM_VOLUME,
        rowPath['paths_uri'],
        self.volume_id,
        False
      ])
      # Save root node for icon change
      if pathRelative == '/':
        folderRoot = subFolder
      paths[pathRelative] = subFolder
      subFolder = None
      # Cancel running thread
      if self.__cancel:
        break

    # Load completed
    curPaths.close()
    paths = {}
    # Completed callback
    self.treeVolumes.set_value(folderRoot, 1, icon_disc)
    self.treeVolumes.set_value(folderRoot, 6, True)
    if self.when_completed:
      self.when_completed(self)

  def cancel(self):
    self.__cancel = True
